package com.lijiajia.cloud.authserver.service.impl.mongodb;

import com.lijiajia.cloud.authserver.service.MenuService;
import com.lijiajia.cloud.authserver.pojo.mongodb.Menu;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 菜单服务 mongo实现
 */
@Service
public class MongoMenuService implements MenuService {
    private final MongoTemplate mongoTemplate;

    public MongoMenuService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public List<Menu> find() {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC, Menu.SORTED));
        return mongoTemplate.find(query, Menu.class);
    }

    @Override
    public Optional<Menu> findById(Long id) {
        Query query = new Query();
        query.addCriteria(Criteria.where(Menu.ID).is(id));
        Menu menu = mongoTemplate.findOne(query, Menu.class);
        return Optional.ofNullable(menu);
    }

    @Override
    public List<Menu> findByIds(Collection<Long> ids) {
        Query query = new Query();
        query.addCriteria(Criteria.where(Menu.ID).in(ids));
        return mongoTemplate.find(query, Menu.class);

    }
}
